package com.ruoyi.manage.enterprise.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StrUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.manage.enterprise.domain.EcEnterpriseProgress;
import com.ruoyi.manage.enterprise.domain.vo.EcEnterpriseProgressVo;
import com.ruoyi.manage.enterprise.service.IEcEnterpriseProgressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
 * 企业认证流程Controller
 *
 * @author ruoyi
 * @date 2025-08-21
 */
@Api(tags="企业认证流程控制器")
@RestController
@RequestMapping("/manage/progress" )
public class EcEnterpriseProgressController extends BaseController {
    /**
    * 配置表别名 默认空
    */
    private final static String TABLE_PREFIX=StrUtil.initTableAlias(null);
    @Autowired
    private IEcEnterpriseProgressService ecEnterpriseProgressService;

    /**
     * 查询企业认证流程列表
     */
    @ApiOperation(value = "查询企业认证流程",notes = "企业认证流程管理")
    @GetMapping("/list" )
    public TableDataInfo list(EcEnterpriseProgressVo ecEnterpriseProgress,QueryWrapper<EcEnterpriseProgress> queryWrapper) {
        setQueryCondition(ecEnterpriseProgress,queryWrapper);
        startPage();
        List<EcEnterpriseProgress> list = ecEnterpriseProgressService.list(queryWrapper);
        return getDataTable(list);
    }
    private void setQueryCondition(EcEnterpriseProgressVo ecEnterpriseProgress, QueryWrapper<EcEnterpriseProgress> queryWrapper) {
        if(ecEnterpriseProgress.getVersion() != null){
            queryWrapper.eq(TABLE_PREFIX+"version" ,ecEnterpriseProgress.getVersion());
        }
        if(ecEnterpriseProgress.getDeleted() != null){
            queryWrapper.eq(TABLE_PREFIX+"deleted" ,ecEnterpriseProgress.getDeleted());
        }
        if(StrUtil.isNotBlank(ecEnterpriseProgress.getProgressState())){
            queryWrapper.eq(TABLE_PREFIX+"progress_state" ,ecEnterpriseProgress.getProgressState());
        }
        if(ecEnterpriseProgress.getRecordTime() != null){
            queryWrapper.eq(TABLE_PREFIX+"record_time" ,ecEnterpriseProgress.getRecordTime());
        }
        if(StrUtil.isNotBlank(ecEnterpriseProgress.getOperator())){
            queryWrapper.eq(TABLE_PREFIX+"operator" ,ecEnterpriseProgress.getOperator());
        }
        if(StrUtil.isNotBlank(ecEnterpriseProgress.getCreditCode())){
            queryWrapper.eq(TABLE_PREFIX+"credit_code" ,ecEnterpriseProgress.getCreditCode());
        }
        if(ecEnterpriseProgress.getEnterpriseId() != null){
            queryWrapper.eq(TABLE_PREFIX+"enterprise_id" ,ecEnterpriseProgress.getEnterpriseId());
        }
        queryWrapper.orderByDesc(TABLE_PREFIX+"create_time");
    }

    /**
     * 导出企业认证流程列表
     */
    @PreAuthorize("@ss.hasPermi('manage:progress:export')" )
    @PostMapping("/export" )
    public void export(HttpServletResponse response, EcEnterpriseProgressVo ecEnterpriseProgress,QueryWrapper<EcEnterpriseProgress> queryWrapper) {
        if (CollUtil.isEmpty( ecEnterpriseProgress.getIds())){
            setQueryCondition(ecEnterpriseProgress,queryWrapper);
        }else {
            queryWrapper.in(TABLE_PREFIX+"id",ecEnterpriseProgress.getIds());
        }
        List<EcEnterpriseProgress> list = ecEnterpriseProgressService.list(queryWrapper);
        ExcelUtil<EcEnterpriseProgress> util = new ExcelUtil<EcEnterpriseProgress>(EcEnterpriseProgress. class);
        util.exportExcel(response,list, "progress");
    }

    /**
     * 获取企业认证流程详细信息
     */
    @ApiOperation(value = "获取企业认证流程",notes = "企业认证流程管理")
    @PreAuthorize("@ss.hasPermi('manage:progress:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(ecEnterpriseProgressService.queryGetById(id));
    }

    /**
     * 新增企业认证流程
     */
    @ApiOperation(value = "新增企业认证流程",notes = "企业认证流程管理")
    @PreAuthorize("@ss.hasPermi('manage:progress:add')" )
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@Validated @RequestBody EcEnterpriseProgress ecEnterpriseProgress) {
        return toAjax(ecEnterpriseProgressService.addEcEnterpriseProgress(ecEnterpriseProgress) ? 1 : 0);
    }

    /**
     * 修改企业认证流程
     */
    @ApiOperation(value = "修改企业认证流程",notes = "企业认证流程管理")
    @PreAuthorize("@ss.hasPermi('manage:progress:edit')" )
    @PutMapping
    public AjaxResult edit(@RequestBody EcEnterpriseProgress ecEnterpriseProgress) {
        return toAjax(ecEnterpriseProgressService.updateEcEnterpriseProgress(ecEnterpriseProgress) ? 1 : 0);
    }

    /**
     * 删除企业认证流程
     */
    @ApiOperation(value = "删除企业认证流程",notes = "企业认证流程管理")
    @PreAuthorize("@ss.hasPermi('manage:progress:remove')" )
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ecEnterpriseProgressService.deleteEcEnterpriseProgressByIds(ids) ? 1 : 0);
    }
}
